import time
import multiprocessing
from artifacts.app.sr.billet_times import once_need_times


process_count = 30
graduate_times_per_process = 500


def expect_need_times():
    process_name = multiprocessing.current_process().name
    times = 0
    gt = graduate_times_per_process
    percent = gt / 100
    for index in range(gt):
        ot = once_need_times(False)
        times += ot
        if index % percent == 0:
            print(f'{process_name} - {index / percent}/100')
    return times / gt


def process_func(m, k):
    m.append(expect_need_times())
    print('process', k, 'complete.')


if __name__ == '__main__':
    t = time.time()
    process_list = []
    rv = multiprocessing.Manager().list()
    for i in range(process_count):
        process_list.append(multiprocessing.Process(target=process_func, args=(rv, i)))
    for process in process_list:
        process.start()
    for process in process_list:
        process.join()

    total = 0
    for i in rv:
        total += i

    print('expect_need_times:', total / process_count)
    print('run in:', time.time() - t, 's')
